<?

$dataup = 0;
$uprun  = true;

/**
* Check the entry
*
* check if model is serNum
* YES: it should have serNum
* NO : should'nt
*/
$Db = new oracleDb;
$Db->init($_SESSION["ORCL"],
           $_SESSION["OPASS"],
           $_SESSION["SID"],
           _NO_COMMENT);

if($Db->connect()){

  $CFetchers->fetchModelId($Db,
                           $_POST["modelEnt"],
                           $SourceDb->fetchValue("srcDbModel"),
                           $modelId);

  $CFetchers->fetchIsSerNumber($Db,
                               $_POST["modelEnt"],
                               $modelId,
                               $SourceDb->fetchValue("srcDbModel"),
                               $isSerNumber);

}else{

  $Misc->usrDie(_DB_CONN_ERROR);
}

if($_POST["qtyEnt"]){

  if($_POST["qtyEnt"] >= 0){

    $opn = _OPNAME_PLUS;
  }else{

    $opn = _OPNAME_MINUS;
  }
}else{

  /*Do nothing*/
}

if($_POST["entry"]){

  if($isSerNumber == 1){

    for($iRow = 0; $iRow < $_SESSION["init_num"]; $iRow++){

      /**
      * Check for each master detail
      *
      */
      if($_POST["priceEnt"][$iRow]   == ''){

        for($iNext = ($iRow + 1); $iNext < $_SESSION["init_num"]; $iNext++ ){

          if($_POST["priceEnt"][$iNext] != ''){

            $uprun = false;

            $Misc->usrError("Invalid Entry at : ".($iNext + ARRAY_NUMBER)." ");

            break;
          }
        }

        if($uprun == false){

          /* out of this loop */
          break;
        }
      }else{

        if($_POST["priceEnt"][$iRow] != ''){

          if($_POST["serNumEnt"][$iRow]){

            /**
             * check out the validity
             */
            $CFetchers->fetchIsSnExists($Db,
                                        $_POST["serNumEnt"][$iRow],
                                        $SourceDb->fetchValue("srcDbSN"),
                                        $isExists);

            if($opn == _OPNAME_PLUS){

              /**
               * make sure that there is no duplicate serial Number
               */
              if($isExists){

                $uprun = false;

                $Misc->usrError("Invalid Entry at : ".($iRow + ARRAY_NUMBER)." <br/>
                                 Duplicate SN Entry ".$_POST["serNumEnt"][$iRow]." ");
              }else{

                $dataup++;
              }
            }else{

              /**
               * make sure the SN is exist
               */
              if(!$isExists){

                $uprun = false;

                $Misc->usrError("Invalid Entry at : ".($iRow + ARRAY_NUMBER)." <br/>
                                 SN is not exists ".$_POST["serNumEnt"][$iRow]." ");
              }else{

                $dataup++;
              }
            }
          }else{

            $uprun = false;

            $Misc->usrError("Invalid Entry at : ".($iRow + ARRAY_NUMBER)." <br/>
                             No SN Entry ".$_POST["serNumEnt"][$iRow]." ");
          }
        }else{

          $uprun = false;

          $Misc->usrError("Invalid Entry at : ".($iRow + ARRAY_NUMBER)." ");
        }
      }
    }
  }else{

    $dataup = 1;
  }
}else{

  if($isSerNumber == 1){

    for($iRow = 0; $iRow < $_SESSION["init_num"]; $iRow++){

      /**
      * Check for each master detail
      *
      */
      if($_POST["priceEnt"][$iRow] == ''){

        for($iNext = ($iRow + 1); $iNext < $_SESSION["init_num"]; $iNext++ ){

          if($_POST["priceEnt"][$iNext] != '' ){

            $uprun = false;

            $Misc->usrError("Invalid Entry at : ".($iNext + ARRAY_NUMBER)." ");

            break;
          }
        }

        if($uprun == false){

          /* out of this loop */
          break;
        }
      }else{

        if($_POST["priceEnt"][$iRow] != '' ){

          $dataup++;
        }else{

          $uprun = false;

          $Misc->usrError("Invalid Entry at : ".($iRow + ARRAY_NUMBER)." ");
        }
      }
    }
  }else{

    $dataup = 1;
  }
}

$Db->dc();


if($uprun == true && $dataup > 0){

	/**
	* We are good with details
	* continue with the header check
	*
	*/
	if($_POST["trDateEnt"] != '' &&
     $_POST["whsEnt"]    != '' &&
     $_POST["modelEnt"]  != '' &&
     $_POST["qtyEnt"]    != ''){

		//everything is good
		//open confirmation form
		if(!$_POST["confirm"] && !$_POST["back"]){

			require("entry_upload_confirm.php");
		}elseif($_POST["back"]){

			require("entry_restore_me.php");
		}else{

			/**
			* Prepare to upload
			* modify mode is on
			*
			*/
			if($_SESSION["modify_mode"] == "Y" && $_SESSION["jid"] != ''){

				require("upmodifytodb.php");
			}elseif($_SESSION["modify_mode"] != "Y"){ //if modify mode is off

				require("upentrytodb.php");
			}else{

				$Misc->usrError("Unknown Upload Task");
			}
		}
	}else{

		$Misc->usrError("Error: header incompleted");

		require("entry_restore_me.php");
	}
}

if($uprun == false){

	echo "Restoring...\n";
	$restore  = 0;
	$uprun    = 0;
	$dataup   = 0;

	require("entry_restore_me.php");
}
?>